package cn.ac.radi.userver.umodel.util;



import org.slf4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

public class Util {

    public static boolean runCommandProcess(String cmd, String pyCodeHome, Logger logger) throws IOException{

        //logger.info("\t[python命令]：" + cmd);

        long ts = System.currentTimeMillis();   //获取开始时间

        //logger.debug("~~~~~~~~~开始~~~~~~~~");

        boolean ret = false;
        Process proc = null;
        try{
            Runtime rt = Runtime.getRuntime();
            if(pyCodeHome!=null){
                proc = rt.exec(cmd, null, new File(pyCodeHome));
            }
            else{
                proc = rt.exec(cmd, null, null);

            }

            ProcessUtil pu = new ProcessUtil();
            pu.writeProcessStream(proc);

            ret = true;


            proc.waitFor();
        }
        catch (IOException e){
            e.printStackTrace();
            throw e;
        }
//        catch (RuntimeException e){
//            e.printStackTrace();
//            throw e;
//        }
//        catch (InterruptedException e){
//            e.printStackTrace();
//            throw e;
//        }
        finally {

            //logger.debug("~~~~~~~~~结束~~~~~~~~");

            long te = System.currentTimeMillis();   //获取结束时间
            //logger.info("运行时间：" + Long.toString(te-ts) + " 毫秒");

            if(proc!=null){
                //logger.info("~~~~~~~~销毁processor~~~~~~~~");
                proc.destroy();
            }

            return ret;
        }
    }

    public static boolean runCommandProcess(String cmd, Logger logger) throws Exception{

        //logger.info("\t[python命令]：" + cmd);
        System.out.println("执行命令 ： " + cmd);

        long ts = System.currentTimeMillis();   //获取开始时间

        //logger.debug("~~~~~~~~~开始~~~~~~~~");

        boolean ret = false;
        Process proc = null;
        try{
            Runtime rt = Runtime.getRuntime();
            proc = rt.exec(cmd, null, null);

            ProcessUtil pu = new ProcessUtil();
            pu.writeProcessStream(proc);

            ret = true;

            proc.waitFor();
        }
        catch (IOException e){
            e.printStackTrace();
            throw e;
        }
//        catch (RuntimeException e){
//            e.printStackTrace();
//            throw e;
//        }
//        catch (InterruptedException e){
//            e.printStackTrace();
//            throw e;
//        }
        finally {

            //logger.debug("~~~~~~~~~结束~~~~~~~~");

            long te = System.currentTimeMillis();   //获取结束时间
            //logger.info("运行时间：" + Long.toString(te-ts) + " 毫秒");

            System.out.println("执行 耗时： "+(te-ts)+"ms");

            if(proc!=null){
                //logger.info("~~~~~~~~销毁processor~~~~~~~~");
                proc.destroy();
            }

            return ret;
        }
    }

    public static boolean isWindows(){
        String os = System.getProperty("os.name");
        return os.toLowerCase().startsWith("win");
    }

}

